<template>
  <el-dialog
      :title="isUpdate?'修改广告机':'添加广告机'"
      :visible="visible"
      width="850px"
      :destroy-on-close="true"
      :lock-scroll="false"
      @update:visible="updateVisible">
    <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">
      <el-row :gutter="15">
        <el-col :lg="12" :md="12">
          <el-form-item label="所属学校:" prop="organization">
            <el-select
                filterable
                clearable
                v-model="form.organization"
                @change="getSceneList"
                size="small"
                placeholder="-请选择所属学校-"
                class="ele-block">
              <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="12">
          <el-form-item label="所属场地:" prop="scene_id">
            <el-select
                filterable
                clearable
                v-model="form.scene_id"
                @change="$forceUpdate()"
                size="small"
                placeholder="-请选择所属场地-"
                class="ele-block">
              <el-option v-for="item in sceneList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="12" :md="12">
          <el-form-item
              label="班牌名称:"
              prop="name">
            <el-input
                :maxlength="20"
                v-model="form.name"
                placeholder="请输入班牌名称"
                clearable/>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="12">
          <el-form-item
              label="设备编码:"
              prop="code">
            <el-input
                :maxlength="20"
                v-model="form.code"
                placeholder="请输入设备编码"
                clearable/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="12" :md="12">
          <el-form-item
              label="账号:"
              prop="account">
            <el-input
                :maxlength="20"
                v-model="form.account"
                placeholder="请输入账号"
                clearable/>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="12">
          <el-form-item
              label="密码:"
              prop="password">
            <el-input
                :maxlength="20"
                v-model="form.password"
                placeholder="请输入密码"
                clearable/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="15">
        <el-col :lg="12" :md="12">
          <el-form-item label="在线状态:">
            <el-radio-group v-model="form.online">
              <el-radio :label="0">离线</el-radio>
              <el-radio :label="1">在线</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="12">
          <el-form-item label="显示顺序:" prop="sort">
            <el-input-number
                :min="0"
                v-model="form.sort"
                placeholder="请输入显示顺序"
                controls-position="right"
                class="ele-fluid ele-text-left"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-form-item label="备注:">
          <el-input
              :rows="3"
              clearable
              type="textarea"
              :maxlength="200"
              v-model="form.note"
              placeholder="请输入备注"/>
        </el-form-item>
      </el-row>
      <el-form-item label="资源类型:">
        <el-radio-group v-model="form.resource_type">
          <el-radio label="img">图片</el-radio>
          <el-radio label="video">视频</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row :gutter="15">
        <el-form-item label="资源列表:" prop="resource">
          <uploadImage v-if="form.resource_type === 'img'" :limit="10" v-model="img"></uploadImage>
          <uploadVideo v-else :limit="10" v-model="video"></uploadVideo>
        </el-form-item>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
          type="primary"
          @click="save"
          :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'
import uploadVideo from '@/components/uploadVideo'

export default {
  props: {
    visible: Boolean,
    data: Object
  },
  components: { uploadImage, uploadVideo },
  data() {
    return {
      form: Object.assign({resource_type: 'img', online: 1}, this.data),
      rules: {
        organization: [
          {required: true, message: '请输入所属学校', trigger: 'blur'}
        ],
        scene_id: [
          {required: true, message: '请输入所属场地', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入广告机名称', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入设备编码', trigger: 'blur'}
        ],
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
        note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
        resource: [
          {required: true, message: '请上传资源', trigger: 'blur'}
        ],
      },
      loading: false,
      isUpdate: false,
      schoolList: [],
      sceneList: [],
      video: [],
      img: []
    }
  },
  watch: {
    data: {
      handler(val) {
        if (val) {
          this.form = Object.assign({}, val)
          if (this.form.resource_type === 'img') {
            this.video = []
            this.img = this.form.resource
          } else {
            this.video = []
            this.img = []
            this.form.resource.forEach(v => {
              this.video.push({url: v})
            })
          }
          this.isUpdate = true
        } else {
          this.form = {resource_type: 'img', online: 1}
          this.video = []
          this.img = []
          this.isUpdate = false
        }
      },
      immediate: true
    },
    video(val) {
      const video = []
      val.forEach(v => {
        video.push(v.url)
      })
      this.form.resource = video
    },
    img(val) {
      this.form.resource = val
    }
  },
  mounted() {
    this.getSchoolList()
  },
  methods: {
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http.post('/ad_equip/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg)
              if (!this.isUpdate) {
                this.form = {}
              }
              this.updateVisible(false)
              this.$emit('done')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(e => {
            this.loading = false
            this.$message.error(e.message)
          });
        } else {
          return false
        }
      })
    },
    updateVisible(value) {
      this.$emit('update:visible', value)
    },
    getSchoolList() {
      if (this.form.organization) {
        this.$forceUpdate()
        this.getSceneList()
      }
      this.$http.post('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    getSceneList() {
      if (this.form.scene_id) {
        this.$forceUpdate()
      }
      this.$http.post('/sitescene/getSceneList', {oid: this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.sceneList = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    }
  }
}
</script>

<style scoped>
</style>
